package ua.com.fesvlast.hotel.controller.managers;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class ConnectionManagerTOMCAT {

	private static ConnectionManagerTOMCAT manager =null;
	static private DataSource ds =null;
	private Connection conn =null;
	static private Context env=null;
	
	static {
		try {
			InitialContext initContext =new InitialContext();
			
			 env=(Context) initContext.lookup("java:comp/env");
			
			ds= (DataSource) env.lookup("jdbc/dblogin");
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	private ConnectionManagerTOMCAT(){
	
	}
	
	public static  ConnectionManagerTOMCAT getConnectionManager(){
		if(manager==null){
			manager =new ConnectionManagerTOMCAT();
		}
		return manager;
	}
	
	public Connection getConnection(){
		if(conn ==null){
			try {
				conn = ds.getConnection();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return conn;
	}
	
	public void closeConnection(){
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
